//
//  FTYTabBar2ViewController.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/29.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "FTYTabBar2ViewController.h"

#import "NTButton.h"
#import "FTYNavigationController.h"
#import "RWHomePageRootViewController.h"
#import "RWCategoryViewController.h"
#import "RWCollectionViewController.h"
#import "RWShoppingCart2ViewController.h"
#import "RWMyViewController.h"
#import "RWCustomerServiceController.h"

#define kTabBarCount 5

@interface FTYTabBar2ViewController ()

@end

@implementation FTYTabBar2ViewController

#pragma mark - View LifeCycle

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self initTabbar];
    [self initControl];
    [self setNotification];
}

-(void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    for (UIView *child in self.tabBar.subviews) {
        if([child isKindOfClass:[UIControl class]])
        {
            [child removeFromSuperview];
        }
    }
}

#pragma mark - Set Methods

-(void)initTabbar{
    
    WeakSelf(ws);
    CATabbarView *tabbar = [[CATabbarView alloc]init];
    tabbar.frame = self.tabBar.bounds;
    tabbar.btnSelectBlock = ^(int to){
        ws.selectedIndex = to;
    };
    [self.tabBar addSubview:tabbar];
    self.tabbar = tabbar;
    
    [self handleThemeChanged];
}

- (void)initControl{
    
    RWHomePageRootViewController *homePageVC = [[RWHomePageRootViewController alloc] init];
    [self setupChildViewController:homePageVC title:@"首页" imageName:@"home_icon" selectedImage:@"home_sel_icon"];
    
    RWCategoryViewController *categoryVC = [[RWCategoryViewController alloc] init];
    [self setupChildViewController:categoryVC title:@"分类" imageName:@"fenlei_icon" selectedImage:@"fenle_sel_icon"];
    
    RWCollectionViewController *collectionVC = [[RWCollectionViewController alloc] init];
    [self setupChildViewController:collectionVC title:@"关注" imageName:@"focus_icon" selectedImage:@"focus_sel_icon"];
    
    RWShoppingCart2ViewController *shoppingCartVC = [[RWShoppingCart2ViewController alloc] init];
    [self setupChildViewController:shoppingCartVC title:@"购物车" imageName:@"shop_icon" selectedImage:@"shop_sel_icon"];
    
    RWMyViewController *myVC = [[RWMyViewController alloc] init];
    [self setupChildViewController:myVC title:@"个人中心" imageName:@"my_icon" selectedImage:@"my_sel_icon"];
    
    RWCustomerServiceController *customVC = [[RWCustomerServiceController alloc] init];
    [self setupChildViewController:customVC title:@"客服" imageName:@"kefu" selectedImage:@"kefu_sel_icon"];
}

#pragma mark - Notification Method

- (void)setNotification{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleThemeChanged) name:@"" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addShopCartNumber) name:@"AddShopCartNumberNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(firstShowShopCartNumber:) name:@"TabbarShopCartNumberShowNotification" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(minusShopCartNumber:) name:@"MinusShopCartNumberNotification" object:nil];
}

#pragma mark - Notificaiton Invoke Methods

- (void)addShopCartNumber{

    NSInteger sum = [self.tabbar.sumLab.text integerValue];
    self.tabbar.sumLab.hidden = NO;
    self.tabbar.sumLab.text = [@(sum + 1) stringValue];
}

- (void)firstShowShopCartNumber:(NSNotification *)notify{

    NSInteger sum = [notify.object[@"number"] integerValue];
    if (sum > 0) {
        self.tabbar.sumLab.hidden = NO;
    } else {
        self.tabbar.sumLab.hidden = YES;
    }
    self.tabbar.sumLab.text = [notify.object[@"number"] stringValue];
}

- (void)minusShopCartNumber:(NSNotification *)notify{

    NSInteger minusSum = [notify.object[@"number"] integerValue];
    NSInteger oldSum = [self.tabbar.sumLab.text integerValue];
    NSInteger newSum = oldSum - minusSum;
    self.tabbar.sumLab.text = [@(newSum) stringValue];
}

-(void)handleThemeChanged{
    
    //    ThemeManager *defaultManager = [ThemeManager sharedInstance];
    //    if ([defaultManager.themeName isEqualToString:@"高贵紫"]) {
    //        [self.tabbar setBackgroundColor:defaultManager.themeColor];
    //    }else{
    //        self.tabbar.backgroundColor = [UIColor whiteColor];
    //    }
}

#pragma mark - Private Invoke Methods

-(void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImage:(NSString *)selectedImage{
    
    //设置控制器属性
    childVc.title = title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    //包装一个导航控制器
    FTYNavigationController *nav = [[FTYNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
    [self.tabbar addTabBarButtonWithItem:childVc.tabBarItem];
}

- (void)selectIndex:(int)index{
    
    [self.tabbar selectIndex:index];
}

#pragma mark - Dealloc

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
